package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/authority/role")
public class RoleHandler {

    @Autowired
    private RoleService roleService;

    @ResponseBody
    @PostMapping("/list")
    public ResultEntity<PageInfo<Role>> getRoleListData(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                                    @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
                                    @RequestParam(value="pageSize", defaultValue="10") Integer pageSize) {
        PageInfo<Role> pageInfo = roleService.getRolePage(keyword, pageNum, pageSize);
        return ResultEntity.successWithData(pageInfo);
    }

    @GetMapping("/list")
    public String getRoleListPage() {
        return "admin-authority-role";
    }

    @ResponseBody
    @PostMapping("/add")
    public ResultEntity<String> addRole(@RequestBody Role role) {
        roleService.saveRole(role);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/update/{roleId}")
    public ResultEntity<Role> getUpdateRole(@PathVariable("roleId") Integer roleId) {
        Role role = roleService.getRoleById(roleId);
        return ResultEntity.successWithData(role);
    }

    @ResponseBody
    @RequestMapping("/update")
    public ResultEntity<String> postUpdateRole(@RequestBody Role role,
                                               @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                               @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
                                               @RequestParam(value="pageSize", defaultValue="10") Integer pageSize) {
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @PostMapping("/delete")
    public ResultEntity<String> postDeleteRoleByIdList(@RequestBody List<Integer> idList) {
        roleService.removeRoleByIdList(idList);
        return ResultEntity.successWithoutData();
    }

    @RequestMapping("/assign/user")
    public String getRoleAssignPage(@RequestParam("adminId") Integer adminId,
                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                    ModelMap modelMap) {
        List<Role> assignRoleList = roleService.getAssignRoleList(adminId);
        List<Role> unAssignRoleList = roleService.getUnAssignRoleList(adminId);
        modelMap.addAttribute("assignRoleList", assignRoleList);
        modelMap.addAttribute("unAssignRoleList", unAssignRoleList);
        return "assign-role";
    }

    @ResponseBody
    @RequestMapping("/auth/get")
    public ResultEntity<List<Auth>> getRoleAuthList(@RequestParam("roleId") Integer id) {
        List<Auth> authList = roleService.getAuthListByRoleId(id);
        return ResultEntity.successWithData(authList);
    }
}
